function calculate() {  
    // 获取输入值  
    var num1 = parseFloat(document.getElementById('num1').value);  
    var num2 = parseFloat(document.getElementById('num2').value);  
    var operator = document.getElementById('operator').value;  
  
    // 验证输入值  
    if (isNaN(num1) || isNaN(num2)) {  
        console.log('Please enter valid numbers!');  
        return;  
    }  
  
    // 根据运算符进行计算  
    var result;  
    switch (operator) {  
        case 'add':  
            result = num1 + num2;  
            break;  
        case 'subtract':  
            result = num1 - num2;  
            break;  
        case 'multiply':  
            result = num1 * num2;  
            break;  
        case 'divide':  
            if (num2 === 0) {  
                console.log('Cannot divide by zero!');  
                return;  
            }  
            result = num1 / num2;  
            break;  
        default:  
            console.log('Invalid operator!');  
            return;  
    }  
  
    // 在控制台显示结果  
    alert('结果为: ' + result);  
}